﻿using Newtonsoft.Json.Linq;
using System.Data;
using System.Text;

namespace CommonUtils.CodeFactory
{
    public static class ClassUtil
    {
        public static string GenerateClassCode(this TableSchema tableSchema)
        {
            if (tableSchema.Name.IsNullOrEmpty())
                tableSchema.Name = "Class1";

            if (tableSchema.Namespace.IsNullOrEmpty())
                tableSchema.Namespace = "CodeFactory";

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine();
            sb.AppendLine("namespace " + tableSchema.Namespace);
            sb.AppendLine("{");
            if (!tableSchema.Comment.IsNullOrEmpty())
            {
                sb.AppendLine("    /// <summary>");
                sb.AppendLine("    /// " + tableSchema.Comment);
                sb.AppendLine("    /// </summary>");
            }
            sb.AppendLine("    public class " + tableSchema.Name);
            sb.AppendLine("    {");

            bool isFirstField = true;
            foreach (var columnSchema in tableSchema.Columns)
            {
                //非首字段添加换行
                if (!isFirstField)
                    sb.AppendLine();
                else
                    isFirstField = false;

                if (!columnSchema.Comment.IsNullOrEmpty())
                {
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// " + columnSchema.Comment);
                    sb.AppendLine("        /// </summary>");
                }
                sb.AppendLine(string.Format("        public {0} {1} ", columnSchema.GetDateTypeIdentifier(), columnSchema.GetFieldName()) + "{ get; set; }");
            }
            sb.AppendLine("    }");
            sb.Append("}");

            return sb.ToString();
        }

        public static string GenerateClassCode(this DataTable table)
        => table.GetSchema().GenerateClassCode();

        public static string GenerateClassCode(this JArray jArray, string className = null, string @namespace = null)
        => jArray.ToTable(className, @namespace).GenerateClassCode();

        public static string GenerateClassCode(this JObject jObject, string className = null, string @namespace = null)
        => new JArray { jObject }.GenerateClassCode(className, @namespace);


        public static void GenerateClassFile(this TableSchema tableSchema, string path)
        => FileUtil.Save(path, tableSchema.GenerateClassCode());

        public static void GenerateClassFile(this DataTable table, string path)
        => FileUtil.Save(path, table.GenerateClassCode());

        public static void GenerateClassFile(this JArray jArray, string path, string className = null, string @namespace = null)
        => FileUtil.Save(path, jArray.GenerateClassCode(className, @namespace));

        public static void GenerateClassFile(this JObject jObject, string path, string className = null, string @namespace = null)
        => FileUtil.Save(path, jObject.GenerateClassCode(className, @namespace));


        public static void GenerateClassFiles(this DbBase db, string folder, string @namespace = null)
        {
            foreach (var tableSchema in db.GetTablesSchemas())
            {
                tableSchema.Namespace = @namespace;
                tableSchema.GenerateClassFile(folder.Combine(tableSchema.Name + ".cs"));
            }
        }
    }
}
